# img code
import sensor, image, lcd, time
import gc, sys
from Maix import utils
import machine

from Maix import GPIO
from fpioa_manager import fm
from machine import UART

UART_TX = 24  # 6 24
UART_RX = 25 # 7 25
api_run = 0
data_send= ''

img_size = [224,224]
board_cube = 1

def lcd_show_except(e):
    import uio
    err_str = uio.StringIO()
    sys.print_exception(e, err_str)
    err_str = err_str.getvalue()
    img = image.Image(size=(img_size[0],img_size[1]))
    img.draw_string(0, 10, err_str, scale=1, color=(0xff,0x00,0x00))
    lcd.display(img)

def draw_string(img, x, y, text, color, scale, bg=None ):
    if bg:
        img.draw_rectangle(x-2,y-2, len(text)*8*scale+4 , 16*scale, fill=True, color=bg)
    img = img.draw_string(x, y, text, color=color,scale=scale)
    return img

def loop(uart_A=None,key_enter=1,key_back=1,key_next=1):
    try:
        global data_send
        img = sensor.snapshot()
        if board_cube:
            img = img.resize(img_size[0], img_size[1])
            img = img.rotation_corr(z_rotation=90)
            img.pix_to_ai()

        res = img.find_qrcodes()
        if len(res) > 0:
                img.draw_string(2,2, "is: %s" %res[0].payload(), color=(0,128,0), scale=3)
                s = str(res[0].payload())
                print(res[0].payload())
                if data_send != s:
                    uart_A.write(s)
                    data_send = s
        img.draw_string(0, 200, "imgCode-6:", color=lcd.RED,scale=2)
        #{"x":37, "y":32, "w":153, "h":154, "payload":"stop", "version":1, "ecc_level":2, "mask":0, "data_type":4, "eci":0}
        lcd.display(img)
        time.sleep_ms(100)

    except Exception as e:
        raise e
    finally:
        del img
        #pass

def work():
    try:
        uart_A = UART(UART.UART1, 115200, 8, 1, 0, timeout=1000, read_buf_len=4096)
        # reg
        fm.register(16, fm.fpioa.GPIOHS0) # 16 NEXT
        fm.register(11, fm.fpioa.GPIOHS1) #11 BACK
        fm.register(10, fm.fpioa.GPIOHS2) #10 ENTER
        key_next = GPIO(GPIO.GPIOHS0, GPIO.PULL_UP) # 0
        key_back = GPIO(GPIO.GPIOHS1, GPIO.PULL_UP) # 0
        key_enter = GPIO(GPIO.GPIOHS2, GPIO.PULL_UP) # 0

        fm.register(UART_TX, fm.fpioa.UART1_TX, force=True) # 6 24
        fm.register(UART_RX, fm.fpioa.UART1_RX, force=True) # 7 25
        time.sleep_ms(100) # wait uart ready
        
        uart_A.write(b'hello world')

        while(True):
            loop(uart_A,key_enter,key_back,key_next)
    except Exception as e:
        sys.print_exception(e)
        lcd_show_except(e)
    finally:
        fm.unregister(16)
        fm.unregister(11)
        fm.unregister(10)
        uart_A.deinit()
        del uart_A
        gc.collect()

def main(sensor_window=(224, 224), lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False):
    sensor.reset()
    sensor.set_pixformat(sensor.RGB565)
    sensor.set_framesize(sensor.QVGA)
    sensor.set_windowing((img_size[0], img_size[1]))
    #sensor.set_hmirror(sensor_hmirror)
    sensor.set_vflip(sensor_vflip)

    if board_cube == 1:
        sensor.set_vflip(True)
        sensor.set_hmirror(True)
        lcd.init(type=2)
        lcd.rotation(2)
    else:
        lcd.init()
        lcd.rotation(lcd_rotation)

    sensor.run(1)
    print("AA start run")
    lcd.clear(lcd.WHITE)
    work()

# api
def run(uart_A=None,key_enter=1,key_back=1,key_next=1):
    try:
        global api_run
        api_run = 1
        while(api_run):
            loop(uart_A,key_enter,key_back,key_next)
    except Exception as e:
        sys.print_exception(e)
    finally:
        gc.collect()

if __name__ == "__main__":
    main()
